import random


# the menu
menu = ['清蒸鲈鱼','宫保鸡丁','鱼香肉丝','蚂蚁上树','水煮肉片','西红柿炖牛腩','干锅牛蛙','杀猪菜']
menuLen = len(menu)

# then menu with prices
prices = []
tup = enumerate(menu, 1)
random.seed(1)
for i, j in tup:
    prices.append([j, int(100 * i * random.random() // 10 * 10 + 10)])
menu = prices

# main functions
promptMainArr = []
promptMainArr.append('查看菜单')
promptMainArr.append('点菜')
promptMainArr.append('结账')
promptMainArr.append('留言')
promptMainArr.append('查看留言')
promptMainArr.append('退出')
promptMainArrLen = len(promptMainArr)
promptStr = '-' * 32 + '\n'
for i, j in enumerate(promptMainArr):
    promptStr += f'{i + 1} - {j}\n'


# main loop
ordered_nums = []  # ordered number of the dishes
comments = []  # comments
while True:
    print(promptStr)
    try:
        choice = int(input('请选择：').strip())
    except ValueError:
        print('输入有误！请重新选择！')
        continue
    if not(1 <= choice <= promptMainArrLen):
        print('输入有误！请重新选择！')
    else:
        # last function, always be exit
        n = promptMainArrLen
        if n == choice:  # quit
            print('欢迎再来！')
            break

        # previous function, check comments
        n -= 1
        if n == choice:
            print('以往顾客评论如下：')
            for c in comments:
                print(f'\t>{c}')
            continue

        # previous function, comment
        n -= 1
        if n == choice:
            comment = input('请留下您宝贵的评论：').strip()
            if len(comment) > 0:
                comments.append(comment)
            continue

        # previous function, pay
        n -= 1
        if n == choice:
            xsum = 0
            for o in ordered_nums:
                xsum += menu[o - 1][1]
            print(f'您一共消费{xsum}元。')
            continue

        # previous function, order
        n -= 1
        if n == choice:
            while True:
                if len(ordered_nums):
                    print('您已经点了如下菜品：')
                    for el in ordered_nums:
                        print(f'{el} - {menu[el - 1][0]}')
                else:
                    print('您目前没有点菜，请输入序号进行点菜。')
                ch = input('您点什么？(输入0结束, 输入a全点，r1 r2 r3...进行删除，ra进行清空。)').strip()
                if 'a' == ch:
                    ordered_nums.extend(range(1, menuLen + 1))
                    continue
                elif 'ra' == ch:
                    ordered_nums.clear()
                    continue
                elif len(ch) >= 2 and 'r' == ch[0]:
                    try:
                        tgt = int(ch[1:])
                        if tgt > menuLen or tgt < 1:
                            print('删除失败！请按菜单序号！')
                            continue
                        try:
                            ordered_nums.remove(tgt)
                        except ValueError:
                            print('删除失败！您并没有点：', tgt, menu[tgt - 1][0])
                            continue
                    except ValueError:
                        print('输入有误！')
                    continue
                try:
                    ch = int(ch)
                except ValueError:
                    print('输入有误！')
                    continue
                if ch == 0:
                    break
                if not(1 <= ch <= menuLen):
                    print('输入有误！请按菜单序号点菜！')
                    continue
                ordered_nums.append(ch)
            continue

        # previous function, check menu
        n -= 1
        if n == choice:
            print('可点菜品如下：')
            for i, j in enumerate(menu, 1):
                print(i, j)
            print()
            continue
